//
//  PopoverPresentationController.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 21/04/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {

    var presentFrame:CGRect = CGRect.zero
    
    /*
     presentedViewController:被展现的VC
     presenting: 发起的VC
     return 
    */
    override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
        super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
        
    }
    /*
        即将布局转场子视图
     */
     override func containerViewWillLayoutSubviews()
    {
        /*
            修改即将展示视图大小
         */
        super.containerViewWillLayoutSubviews()
        if presentFrame == CGRect.zero {
            presentedView?.frame = CGRect(x: 100*KTC_SCREEN_RATION, y: 44+SAFE_SCREEN_TOP, width: 200*KTC_SCREEN_RATION, height: 200*KTC_SCREEN_RATION)
        }else{
            presentedView?.frame = presentFrame
        }
        containerView?.insertSubview(coverView, at: 0)
    }
    
    //MARK:
    private lazy var coverView:UIView = {
       let view = UIView()
        view.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        view.frame = UIScreen.main.bounds
        // 创建手势监听
        let tap = UITapGestureRecognizer(target: self, action: #selector(closePresentedView))
        view.addGestureRecognizer(tap)
        return view
    }()
    @objc private func closePresentedView(){
       presentedViewController.dismiss(animated: true, completion: nil)
    }
   
}
